package com.tianyu.inspection.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tianyu.inspection.common.component.AppConstants;
import com.tianyu.inspection.common.component.MapperModel;
import com.tianyu.inspection.common.model.PageModel;
import com.tianyu.inspection.entity.ConstructionType;
import com.tianyu.inspection.entity.ConstructionUnits;
import com.tianyu.inspection.entity.model.OperateMo;
import com.tianyu.inspection.entity.model.QueryMo;
import com.tianyu.inspection.mapper.ConstructionUnitsMapper;
import com.tianyu.inspection.service.api.IConstructionUnitsService;
import io.ocs.model.OcsSimpleModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

/**********************************
 * @Author YSW
 * @Description 基础设施服务
 * @Date 2021/1/18 - 09:32
 **********************************/

@Service
public class ConstructionUnitsServiceImpl implements IConstructionUnitsService {

    @Resource
    private MapperModel mapperModel;

    @Resource
    private ConstructionUnitsMapper constructionUnitsMapper;

    @Resource
    private AppConstants appConstants;

    @Resource
    private OcsSimpleModel ocsSimpleModel;

    /**
     * 分页查询
     *
     * @param queryMo 参数模型
     * @return 分页记录对象
     */
    @Override
    public IPage<ConstructionUnits> searchListPage(QueryMo queryMo) {
        Map<Integer, String> pipelineMap = mapperModel.getPipelineMap(queryMo);
        Map<Integer, String> pipelineStationMap = mapperModel.getPipelineStationMap(queryMo);
        Map<Integer, ConstructionType> constructionTypeMap = mapperModel.getConstructionTypeMap();
        IPage<ConstructionUnits> page = PageModel.getModelPage(queryMo, constructionUnitsMapper);
        page.getRecords().forEach(constructionUnits -> {
            final ConstructionType constructionType = constructionTypeMap.get(constructionUnits.getConstructionTypeId());
            if (Objects.nonNull(constructionType)){
                constructionUnits.setConstructionTypeName(constructionType.getName());
                final String icon = constructionType.getIcon();
                if (Objects.nonNull(icon)){
                    constructionUnits.setConstructionTypeIcon(ocsSimpleModel.getObjectUrl(icon));
                }
            }
            constructionUnits.setPipelineName(pipelineMap.get(constructionUnits.getPipelineId()));
            constructionUnits.setPipelineStationName(pipelineStationMap.get(constructionUnits.getPipelineStationId()));
            if (Objects.nonNull(constructionUnits.getSceneImage())){
                constructionUnits.setSceneImage(ocsSimpleModel.getObjectUrl(constructionUnits.getSceneImage()));
            }
            if (Objects.nonNull(constructionUnits.getSafeAreaImage())){
                constructionUnits.setSafeAreaImage(ocsSimpleModel.getObjectUrl(constructionUnits.getSafeAreaImage()));
            }
        });
        return page;
    }

    /**
     * 通过ID等参数查询一条数据
     *
     * @param queryMo 参数集[实体ID 与 项目ID, 数据状态等]
     * @return 单条记录结果
     */
    @Override
    public ConstructionUnits selectOne(QueryMo queryMo) {
        if (Objects.nonNull(queryMo.getEqMap().get("id"))){
            ConstructionUnits constructionUnits = constructionUnitsMapper.selectOne(new QueryWrapper<ConstructionUnits>().allEq(queryMo.getEqMap()));
            if (Objects.nonNull(constructionUnits)){
                if (Objects.nonNull(constructionUnits.getSceneImage())){
                    constructionUnits.setSceneImage(ocsSimpleModel.getObjectUrl(constructionUnits.getSceneImage()));
                }
                if (Objects.nonNull(constructionUnits.getSafeAreaImage())){
                    constructionUnits.setSafeAreaImage(ocsSimpleModel.getObjectUrl(constructionUnits.getSafeAreaImage()));
                }
            }
            return constructionUnits;
        }
        return null;
    }

    /**
     * 插入一条数据
     *
     * @param operateMo 实体对象
     * @return 是否操作成功
     */
    @Override
    public boolean insert(OperateMo operateMo) {
        ObjectMapper objectMapper = new ObjectMapper();
        ConstructionUnits constructionUnits = objectMapper.convertValue(operateMo.getEntity(), ConstructionUnits.class);
        return 1 == constructionUnitsMapper.insert(constructionUnits);
    }

    /**
     * 通过ID删除一条数据
     *
     * @param queryMo 参数集[实体ID 与 项目ID, 数据状态等]
     * @return 是否操作成功
     */
    @Override
    public boolean delete(QueryMo queryMo) {
        if(Objects.nonNull(queryMo.getEqMap().get("id"))){
            return 1 == constructionUnitsMapper.delete(new QueryWrapper<ConstructionUnits>().allEq(queryMo.getEqMap()));
        }
        return false;
    }

    /**
     * 通过ID更新一条数据
     *
     * @param operateMo 实体对象参数集
     * @return 是否操作成功
     */
    @Override
    public boolean update(OperateMo operateMo) {
        ObjectMapper objectMapper = new ObjectMapper();
        ConstructionUnits constructionUnits = objectMapper.convertValue(operateMo.getEntity(), ConstructionUnits.class);
        return 1 == constructionUnitsMapper.update(constructionUnits,new QueryWrapper<ConstructionUnits>().allEq(operateMo.getEqMap()));
    }

    /**
     * 通过ID逻辑删除一条数据
     *
     * @param queryMo 参数集[实体ID 与 项目ID, 数据状态等]
     * @return 是否操作成功
     */
    @Override
    public boolean logicallyDelete(QueryMo queryMo) {
        return 1 == constructionUnitsMapper.update(
                new ConstructionUnits().setState(1),
                new QueryWrapper<ConstructionUnits>().allEq(queryMo.getEqMap())
        );
    }
}
